package Chapter14.Homework;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author 杨森
 * @date 2022年10月27日 14:01
 */
public class HashSetExercise {
    public static void main(String[] args) {
//        如果name和age相同 这加入到链表当中
        HashSet hashSet = new HashSet();

        hashSet.add(new Employee("jack", 20));
        hashSet.add(new Employee("tom", 20));
        hashSet.add(new Employee("jack", 20));

        System.out.println("hash="+hashSet);

    }
}

class Employee {
    private String name;
    private int age;

    public Employee() {
    }

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Employee{");
        sb.append("name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append('}');
        return sb.toString();
    }


//    如果name和age值相同，则返回相同的hash值
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Employee employee = (Employee) o;
        return age == employee.age && Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
